package com.hulman.oms.web;

import com.hulman.oms.bean.Result;
import com.hulman.oms.bean.ShifterReport;
import com.hulman.oms.service.SSEService;
import com.hulman.oms.service.ShifterReportService;
import com.hulman.oms.util.DateUtil;
import com.hulman.oms.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: maxwellens
 */
@Slf4j
@RestController
public class ShifterReportController
{
    @Autowired
    private ShifterReportService shifterReportService;
    @Autowired
    private SSEService sseService;

    @GetMapping("/shifter-reports/{id}")
    public Result findShifterReportById(@PathVariable Integer id)
    {
        ShifterReport shifterReport = shifterReportService.findShifterReportById(id);
        return new Result(shifterReport);
    }

    @GetMapping("/shifter-reports")
    public Result findShifterReports(Integer page, Integer limit, String content, String coordination, String matter, String dateRange, Integer shifterId, String shifterName, Integer state)
    {
        Map<String, Object> map = new HashMap<>();

        map.put("page", page);
        map.put("limit", limit);
        map.put("content", content);
        map.put("coordination", coordination);
        map.put("matter", matter);
        if (!StringUtil.isEmpty(dateRange))
        {
            String[] dates = dateRange.split(" - ");
            map.put("startDate", dates[0]);
            map.put("endDate", dates[1]);
        }
        map.put("shifterId", shifterId);
        map.put("shifterName", shifterName);
        map.put("state", state);
        return shifterReportService.findShifterReportsResult(map);
    }

    @DeleteMapping("/shifter-reports/{id}")
    public Result deleteShifterReportById(@PathVariable Integer id)
    {
        shifterReportService.deleteShifterReportById(id);
        return Result.SUCCESS;
    }

    @PutMapping("/shifter-reports")
    public Result saveShifterReport(ShifterReport shifterReport)
    {
        shifterReportService.saveShifterReport(shifterReport);
        return Result.SUCCESS;
    }

    @GetMapping("/shifter-reports/create")
    public Result create()
    {
        ShifterReport shifterReport = shifterReportService.createTodayShifterReport();
        return new Result(shifterReport);
    }

    @PostMapping("/shifter-reports/generate")
    public Result generate(ShifterReport shifterReport) throws IOException
    {
        shifterReportService.generate(shifterReport);
        return Result.SUCCESS;
    }

    @PostMapping("/shifter-reports/submit")
    public Result submit(ShifterReport shifterReport) throws IOException
    {
        shifterReportService.submit(shifterReport);
        return Result.SUCCESS;
    }

    @GetMapping("/shifter-reports/generate-content")
    public ResponseBodyEmitter generateContent(String date)
    {
        String userPrompt = "根据以下给出的内容概括总结今日值班内容，简洁扼要，不超过100字：\n" + shifterReportService.getWorkContents(DateUtil.parseDate(date));
        ResponseBodyEmitter emitter = sseService.emitter(userPrompt);
        return emitter;
    }

    @GetMapping("/shifter-reports/generate-coordination")
    public ResponseBodyEmitter generateCoordination(String date)
    {
        String userPrompt = "根据以下内容，发现协调事项，简洁扼要，不超过100字：\n" + shifterReportService.getWorkContents(DateUtil.parseDate(date));
        ResponseBodyEmitter emitter = sseService.emitter(userPrompt);
        return emitter;
    }

    @GetMapping("/shifter-reports/generate-matter")
    public ResponseBodyEmitter generateMatter(String date)
    {
        String userPrompt = "根据以下内容，发现重大事项，简洁扼要，不超过100字：\n" + shifterReportService.getWorkContents(DateUtil.parseDate(date));
        ResponseBodyEmitter emitter = sseService.emitter(userPrompt);
        return emitter;
    }

}
